<template>
  <el-dropdown @command="handleCommand">
    <span class="iconfont icon-qiehuan" />
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item
          v-for="(item, index) in irrTypes"
          :key="index"
          :command="item.value"
        >
          {{ item.name }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script>
  import { doIrrType } from '@/api/authUser'
  import { irrTypes } from '@/utils/constant'
  import { convertArr2Obj } from '@/utils'
  export default {
    name: 'VabSwitchIrrType',
    data() {
      return {
        irrTypes,
      }
    },
    methods: {
      handleCommand(command) {
        const irrTypesObj = convertArr2Obj(irrTypes, 'value')
        const irrType = irrTypesObj[command]
        this.$baseConfirm(
          `确定要切换灌溉模式为 ${irrType.name} 吗？`,
          null,
          async () => {
            const { msg } = await doIrrType(command)
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            setTimeout(() => {
              window.location.reload()
            }, 500)
          }
        )
      },
    },
  }
</script>
<style lang="scss" scoped>
  .iconfont {
    display: inline-block;
    margin-left: 20px;
    font-size: 18px;
    color: rgba(0, 0, 0, 0.65);
    text-align: center;
    vertical-align: -3.5px;
    cursor: pointer;
  }
</style>
